//
//  XXTGRedBagBox.m
//  XiuXiuTuanGou
//
//  Created by 张敬 on 2018/12/19.
//  Copyright © 2018年 XiuXiuTuanGou. All rights reserved.
//

@interface MeatMarketSwitchModel : NSObject

@property (nonatomic, copy) NSString *name;
@property (nonatomic, assign) BOOL isChoosed;
@property (nonatomic, assign) NSInteger id;

@end

@implementation MeatMarketSwitchModel

@end


#import "MeatMainScreeningBox.h"


@interface MeatMainScreeningBox()

@property (nonatomic, strong) UIScrollView *scrollView;

@property (nonatomic, strong) UILabel *shopperNameLab; //店主姓名
@property (nonatomic, strong) UILabel *shopperAttributeLab; //店主属性
@property (nonatomic, strong) UILabel *deliveryTimeLab; //发货时间
@property (nonatomic, strong) UILabel *partTypesLab; //部位种类
@property (nonatomic, strong) UILabel *pigTypeLab; //猪品种

@property (nonatomic, strong) UIButton *resetBtn; //查看按钮
@property (nonatomic, strong) UIButton *okBtn; // 确定按钮

@property (nonatomic, strong) UIView *bottomView;

@property (nonatomic, strong) NSMutableArray *shopperNameArray;
@property (nonatomic, strong) NSMutableArray *shopperAttributeArray;
@property (nonatomic, strong) NSMutableArray *deliveryTimeArray;
@property (nonatomic, strong) NSMutableArray *partTypesArray;
@property (nonatomic, strong) NSMutableArray *pigTypeArray;


@end

@implementation MeatMainScreeningBox

#pragma mark - lifecycle methods

- (void)setupUI{
    
    [super setupUI];
    
    self.contentView.backgroundColor = [UIColor colorWithRed:0 green:0 blue:0 alpha:0.5];
    [self.contentView addSubview:self.scrollView];
}

- (void)setupContentLayout{
    
    self.contentView.mj_x = 0;
    self.contentView.mj_y = [self fitSize:230];
    self.contentView.width = KScreenWidth;
    self.contentView.height = KScreenHeight - [self fitSize:230];
}


- (void)postMeatScreeningHideNotification{
    
    [kNotificationCenter postNotificationName:kMeatMarketSwitchNotification object:nil];
}


#pragma mark - response

//确定按钮点击事件
- (void)didOkBtnChicked:(id)sender{
    [self dismissView];
}

#pragma mark - setter and getter

- (UIScrollView *)scrollView{
    if (!_scrollView) {
        _scrollView = [[UIScrollView alloc] init];
        _scrollView.frame = CGRectMake(0, 0, KScreenWidth, KScreenHeight - [self fitSize:230]);
        _scrollView.backgroundColor = KWhiteColor;
        
        [_scrollView addSubview:self.shopperNameLab];
        [_scrollView addSubview:self.shopperAttributeLab];
        [_scrollView addSubview:self.deliveryTimeLab];
        [_scrollView addSubview:self.partTypesLab];
        [_scrollView addSubview:self.pigTypeLab];
        
        _shopperNameLab.mj_y = 20;
        _shopperAttributeLab.mj_y = _shopperNameLab.bottom + 75;
        _deliveryTimeLab.mj_y = _shopperAttributeLab.bottom + 75;
        _partTypesLab.mj_y = _deliveryTimeLab.bottom + 75;
        _pigTypeLab.mj_y = _partTypesLab.bottom + 184;
        
        [_scrollView addSubview:self.bottomView];
        _bottomView.mj_y = _pigTypeLab.bottom + 75;
        
        _scrollView.contentSize = CGSizeMake(KScreenWidth, _bottomView.bottom);
        
        //添加可选的btn
        kWeakSelf(self);
        CGFloat gap = (KScreenWidth - 74*4 - 30)/4.0;
        [self.shopperNameArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            UIButton *shopperNameBtn = [weakself createCommonBtn];
            MeatMarketSwitchModel *model = (MeatMarketSwitchModel *)obj;
            [shopperNameBtn setTitle:model.name forState:UIControlStateNormal];
            [weakself.scrollView addSubview:shopperNameBtn];
            shopperNameBtn.mj_x = 15 + idx*(shopperNameBtn.width + gap);
            shopperNameBtn.mj_y = weakself.shopperNameLab.bottom + 10;
            
            if (model.isChoosed) {
                [weakself setupBtnChoosedState:shopperNameBtn];
            }
            
        }];
        
        [self.shopperAttributeArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            UIButton *shopperAttributeBtn = [weakself createCommonBtn];
            MeatMarketSwitchModel *model = (MeatMarketSwitchModel *)obj;
            [shopperAttributeBtn setTitle:model.name forState:UIControlStateNormal];
            [weakself.scrollView addSubview:shopperAttributeBtn];
            shopperAttributeBtn.mj_x = 15 + idx*(shopperAttributeBtn.width + gap);
            shopperAttributeBtn.mj_y = weakself.shopperAttributeLab.bottom + 10;
            
            if (model.isChoosed) {
                [weakself setupBtnChoosedState:shopperAttributeBtn];
            }
        }];

        
        [self.partTypesArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            UIButton *partTypeBtn = [weakself createCommonBtn];
            MeatMarketSwitchModel *model = (MeatMarketSwitchModel *)obj;
            [partTypeBtn setTitle:model.name forState:UIControlStateNormal];
            [weakself.scrollView addSubview:partTypeBtn];
            partTypeBtn.mj_x = 15 + (idx%4)*(partTypeBtn.width + gap);
            partTypeBtn.mj_y = weakself.partTypesLab.bottom + 10 + idx/4 *(36 +18);
            
            if (model.isChoosed) {
                [weakself setupBtnChoosedState:partTypeBtn];
            }
        }];
        
        
        [self.pigTypeArray enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
            
            UIButton *pigTypeBtn = [weakself createCommonBtn];
            MeatMarketSwitchModel *model = (MeatMarketSwitchModel *)obj;
            [pigTypeBtn setTitle:model.name forState:UIControlStateNormal];
            [weakself.scrollView addSubview:pigTypeBtn];
            pigTypeBtn.mj_x = 15 + (idx%4)*(pigTypeBtn.width + gap);
            pigTypeBtn.mj_y = weakself.pigTypeLab.bottom + 10;
            
            if (model.isChoosed) {
                [weakself setupBtnChoosedState:pigTypeBtn];
            }
        }];
    }
    return _scrollView;
}

//店主性别
- (UILabel *)shopperNameLab{
    
    if (!_shopperNameLab) {
        _shopperNameLab = [self createCommonLabel];
        _shopperNameLab.text = @"店主性别";
    }
    return _shopperNameLab;
}

//店主属性
- (UILabel *)shopperAttributeLab{
    
    if (!_shopperAttributeLab) {
        _shopperAttributeLab = [self createCommonLabel];
        _shopperAttributeLab.text = @"店主属性";
    }
    return _shopperAttributeLab;
}

//发货时间
- (UILabel *)deliveryTimeLab{
    
    if (!_deliveryTimeLab) {
        _deliveryTimeLab = [self createCommonLabel];
        _deliveryTimeLab.text = @"发货时间";
    }
    return _deliveryTimeLab;
}

//部位种类
- (UILabel *)partTypesLab{
    
    if (!_partTypesLab) {
        _partTypesLab = [self createCommonLabel];
        _partTypesLab.text = @"部位种类";
    }
    return _partTypesLab;
}


//猪品种
- (UILabel *)pigTypeLab{
    
    if (!_pigTypeLab) {
        _pigTypeLab = [self createCommonLabel];
        _pigTypeLab.text = @"猪品种";
    }
    return _pigTypeLab;
}


- (UIView *)bottomView{
    
    if (!_bottomView) {
        _bottomView = [UIView new];
        _bottomView.size = CGSizeMake(KScreenWidth, 68);
        
        [_bottomView addSubview:self.resetBtn];
        [_bottomView addSubview:self.okBtn];
        
        _resetBtn.mj_x = (KScreenWidth - 15 - _resetBtn.width*2)/2.0;
        _resetBtn.mj_y = (68 - _resetBtn.height)/2.0;
        
        _okBtn.mj_x = _resetBtn.right + 15;
        _okBtn.mj_y = _resetBtn.mj_y;
        
        CAShapeLayer *line = [CAShapeLayer layer];
        UIBezierPath *path = [UIBezierPath bezierPathWithRect:CGRectMake(0, 0, KScreenWidth, 1)];
        line.fillColor = UIColorFromRGB(0xF2F2F2).CGColor;
        line.path = path.CGPath;
        
        [_bottomView.layer addSublayer:line];
        
    }
    return _bottomView;
}


//继续捞红包按钮
- (UIButton *)okBtn{
    if (!_okBtn) {
        
        _okBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_okBtn setTitle:@"确定" forState:UIControlStateNormal];
        [_okBtn setTitleColor:KWhiteColor forState:UIControlStateNormal];
        _okBtn.titleLabel.font = mFont(14);
        _okBtn.size = CGSizeMake(134 ,38);
        _okBtn.backgroundColor = mMainColor;
        [self addShadowAndCircleCorner:_okBtn.layer corner:5];
        [_okBtn addTarget:self action:@selector(didOkBtnChicked:) forControlEvents:UIControlEventTouchUpInside];
    }
    return _okBtn;
}

//重置
- (UIButton *)resetBtn{
    
    if (!_resetBtn) {
        _resetBtn = [UIButton buttonWithType:UIButtonTypeCustom];
        [_resetBtn setTitle:@"重置" forState:UIControlStateNormal];
        [_resetBtn setTitleColor:UIColorFromRGB(0xC0C0C0) forState:UIControlStateNormal];
        _resetBtn.titleLabel.font = mFont(14);
        _resetBtn.backgroundColor = KWhiteColor;
        _resetBtn.size = CGSizeMake(134, 38);
        _resetBtn.layer.borderColor = UIColorFromRGB(0xD8D8D8).CGColor;//设置边框颜色
        _resetBtn.layer.borderWidth = 1.0f;//设置边框颜色
        
        [self addShadowAndCircleCorner:_resetBtn.layer corner:5];
        [_resetBtn addTarget:self action:@selector(dismissView) forControlEvents:UIControlEventTouchUpInside];
    }
    return _resetBtn;
}


- (NSMutableArray *)shopperNameArray{
    if (!_shopperNameArray) {
        
        _shopperNameArray = [NSMutableArray array];
        NSArray *array = @[@"全部", @"男性", @"女性"];
        
        for (NSInteger i = 0; i < array.count; i++) {
            MeatMarketSwitchModel *model = [[MeatMarketSwitchModel alloc] init];
            model.name = array[i];
            model.isChoosed = i == 0 ?YES :NO;
            model.id = i + 100;
            [_shopperNameArray addObject:model];
        }
    }
    return _shopperNameArray;
}

- (NSMutableArray *)shopperAttributeArray{
    if (!_shopperAttributeArray) {
        
        _shopperAttributeArray = [NSMutableArray array];
        NSArray *array = @[@"全部", @"好友", @"非好友"];
        
        for (NSInteger i = 0; i < array.count; i++) {
            MeatMarketSwitchModel *model = [[MeatMarketSwitchModel alloc] init];
            model.name = array[i];
            model.isChoosed = i == 0 ?YES :NO;
            model.id = i + 200;
            [_shopperAttributeArray addObject:model];
        }
    }
    return _shopperAttributeArray;
}


- (NSMutableArray *)partTypesArray{
    if (!_partTypesArray) {

        _partTypesArray = [NSMutableArray array];
        NSArray *array = @[@"全部", @"里脊肉", @"排骨肉", @"五花肉",
                           @"前排肉", @"排骨肉", @"猪尖肉", @"前猪蹄",
                           @"夹心肉", @"坐臀肉", @"大棒骨", @"猪头肉"];

        for (NSInteger i = 0; i < array.count; i++) {
            MeatMarketSwitchModel *model = [[MeatMarketSwitchModel alloc] init];
            model.name = array[i];
            model.isChoosed = i == 0 ?YES :NO;
            model.id = i + 400;
            [_partTypesArray addObject:model];
        }
    }
    return _partTypesArray;
}


- (NSMutableArray *)pigTypeArray{
    if (!_pigTypeArray) {
        
        _pigTypeArray = [NSMutableArray array];
        NSArray *array = @[@"全部", @"白猪", @"黑猪"];
        
        for (NSInteger i = 0; i < array.count; i++) {
            MeatMarketSwitchModel *model = [[MeatMarketSwitchModel alloc] init];
            model.name = array[i];
            model.isChoosed = i == 0 ?YES :NO;
            model.id = i + 500;
            [_pigTypeArray addObject:model];
        }
    }
    return _pigTypeArray;
}


@end
